package com.nhim.cms.admin.server;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.nhim.cms.admin.client.rpc.ContentTypeRPCService;
import com.nhim.cms.admin.shared.ContentTypeRPCProxy;
import com.nhim.cms.dao.ContentTypeDAO;
import com.nhim.cms.domain.ContentType;
import com.nhim.cms.util.GroupManager;

public class ContentTypeRPCServiceImpl extends RemoteServiceServlet implements
		ContentTypeRPCService {

	private static final long serialVersionUID = 1L;

	public List<ContentTypeRPCProxy> findAll() throws IllegalArgumentException {
	
		List<ContentType> allContentTypes = ContentTypeDAO.getInstance()
				.findAll();
		List<ContentTypeRPCProxy> result = new ArrayList<ContentTypeRPCProxy>();
		if (allContentTypes != null && allContentTypes.size() > 0) {
			for (ContentType cType : allContentTypes)
				result.add(toProxy(cType));

		}
		return result;
	}

	private ContentTypeRPCProxy toProxy(ContentType cType) {
		return new ContentTypeRPCProxy(cType.getId(), cType.getName());

	}

	public List<ContentTypeRPCProxy> findByGroupId(Integer groupId)
			throws IllegalArgumentException {
		List<ContentTypeRPCProxy> result = new ArrayList<ContentTypeRPCProxy>();
		try {
			List<ContentType> allContentTypes = ContentTypeDAO.getInstance()
					.findByGroupId(groupId);
			if (allContentTypes != null && allContentTypes.size() > 0) {
				for (ContentType cType : allContentTypes)
					result.add(toProxy(cType));

			}

		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
		return result;
	}

	public Boolean persitGroupContentTypes(Integer groupId,Set<Integer> listContentType) throws IllegalArgumentException {
		try
		{
			return GroupManager.getInstance().persitGroupContentTypes(groupId, listContentType);
		}catch(Exception ex)
		{
			return false;
		}
	}

}
